<template>
  <c-back-wrap :loading="loading" go-page="市场开发/升级周进度跟进" current-page="审核">
    <QupFormContainer :column="3">
      <el-form ref="auditForm" :model="formData" :rules="formRules" label-width="120px">
        <el-form-item prop="aduitStatus" label="审核结果">
          <c-dict-select v-model="formData.aduitStatus" dict-type-code="REVIEW_RESULT" clearable placeholder="审核结果" :exclude="['2']" style="width:100%" />
        </el-form-item>
        <el-form-item prop="aduitFialRemark" label="审核意见" :span="24">
          <el-input v-model="formData.aduitFialRemark" type="textarea" show-word-limit maxlength="1024" :autosize="{minRows: 3}" placeholder="审核意见" clearable />
        </el-form-item>
      </el-form>
    </QupFormContainer>
    <EditForm v-if="[1, '1'].includes(formData.aduitStatus)" ref="form" is-audit :detail="detail" />
    <Week v-else :detail="detail" inner-field />
    <div class="btn-center">
      <c-button type="default" @click="handleCancel">取消</c-button>
      <c-button type="primary" @click="handleConfirm">确定</c-button>
    </div>
  </c-back-wrap>
</template>
<script>
import { weekTaskDetail, auditWeekTask } from '@/api/channelTask2/weekTask'
import Week from './week'
import EditForm from './EditForm'
export default {
  name: 'Audit',
  components: {
    Week,
    EditForm
  },
  props: {
    id: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      formData: {
        aduitStatus: '',
        aduitFialRemark: ''
      },
      detail: {},
      loading: false
    }
  },
  computed: {
    formRules() {
      return {
        aduitStatus: [{ required: true, message: '请选择审核结果', trigger: 'change' }],
        aduitFialRemark: [{ required: [0, '0'].includes(this.formData.aduitStatus), message: '请输入审核意见', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getDetailData()
  },
  methods: {
    getDetailData() {
      this.loading = true
      return weekTaskDetail(this.id).then(res => {
        this.detail = res.data
      }).finally(() => {
        this.loading = false
      })
    },
    handleCancel() {
      this.$pageStack.pop()
    },
    async handleConfirm() {
      await this.$refs.auditForm.validate()
      let ctmShopBuildWeeklyTaskFollowUpDTO = this.detail.ctmShopBuildWeeklyTaskFollowUpDTO
      if ([1, '1'].includes(this.formData.aduitStatus)) {
        ctmShopBuildWeeklyTaskFollowUpDTO = await this.$refs.form.validate()
      }
      const data = {
        ...this.formData,
        shopBuildWeekTaskId: this.id,
        id: this.id,
        ctmShopBuildWeeklyTaskFollowUpDTO
      }
      return auditWeekTask(data).then(() => {
        this.$message.success('操作成功!')
        this.$pageStack.pop('onPage')
      })
    }
  }
}
</script>
